package app.bedcheck;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import app.thread.HdSensingData;
import app.thread.HdSetting;
import app.util.DateUtil;
import app.util.StringUtil;
import base.dao.HibernateDao;
/**
 * 处理类
 * @author zoe
 *
 */
@Service
public class StatisticsService{
	
	@Autowired
	private HibernateDao<HdSensingData> hibernateDao;

	/**
	 * 获取人员在位情况
	 * @param type
	 * @return
	 */
	public List<List<Long>> getPersonState(String type) {
		
		HdSetting hdSetting = getHdSetting();
		
		Integer standardValue = hdSetting.getStandardValue();
		
		String sql = "SELECT a.COLLECT_TIME,a.FINAL_DATA FROM hd_sensing_data a WHERE TO_DAYS(a.COLLECT_TIME) = TO_DAYS(NOW()) ORDER BY a.COLLECT_TIME ASC";
//		String sql = "SELECT a.COLLECT_TIME,a.FINAL_DATA FROM hd_sensing_data a ORDER BY a.COLLECT_TIME ASC";
		List<Map<String, Object>> list = hibernateDao.queryListBySql(sql);
		List<List<Long>> returnList = new ArrayList<List<Long>>();
		for (int i = 0; i < list.size(); i++) {
			List<Long> tempList = new ArrayList<Long>();
			Map<String, Object> map = list.get(i);
			tempList.add(DateUtil.parseDateTime(map.get("collectTime")+"").getTime());
			
			Integer finalData = new Integer(map.get("finalData")+"");
			
			if(finalData<standardValue){
				tempList.add(new Long("1"));
			}else{
				tempList.add(new Long("0"));
			}
			returnList.add(tempList);
		}
		return returnList;
	}
	
	/**
	 * 获取系统参数
	 * @return
	 */
	public HdSetting getHdSetting(){
		String sql = "SELECT * FROM hd_setting a ORDER BY create_time DESC LIMIT 1";
		HdSetting hdsetting = hibernateDao.queryEntityBySql(sql, HdSetting.class);
		return hdsetting!=null?hdsetting:new HdSetting();
	}

	/**
	 * 获取人员压力数据
	 * @param type
	 * @return
	 */
	public List<List<Long>> getPersonData(String type) {
		
		String sql = "SELECT a.COLLECT_TIME,a.FINAL_DATA FROM hd_sensing_data a WHERE TO_DAYS(a.COLLECT_TIME) = TO_DAYS(NOW()) ORDER BY a.COLLECT_TIME ASC";
//		String sql = "SELECT a.COLLECT_TIME,a.FINAL_DATA FROM hd_sensing_data a ORDER BY a.COLLECT_TIME ASC";
		List<Map<String, Object>> list = hibernateDao.queryListBySql(sql);
		List<List<Long>> returnList = new ArrayList<List<Long>>();
		for (int i = 0; i < list.size(); i++) {
			List<Long> tempList = new ArrayList<Long>();
			Map<String, Object> map = list.get(i);
			tempList.add(DateUtil.parseDateTime(map.get("collectTime")+"").getTime());
			tempList.add(new Long(map.get("finalData")+""));
			returnList.add(tempList);
		}
		return returnList;
	}

	/**
	 * 系统设置保存
	 * @param hdSetting
	 * @return
	 */
	public HdSetting saveSetting(HdSetting hdSetting) {
		if(StringUtil.isEmpty(hdSetting.getId())){
			hibernateDao.save(hdSetting);
		}else{
			hibernateDao.mergeAndUpdate(hdSetting);
		}
		
		return hdSetting;
	}

}
